﻿using System.ComponentModel.Composition;
using System.Linq;

namespace WeavingCommon
{
    [Export, PartCreationPolicy(CreationPolicy.Shared)]
	public class ReferenceCleaner
	{
        ModuleReader moduleReader;
        Logger logger;

        [ImportingConstructor]
        public ReferenceCleaner(ModuleReader moduleReader, Logger logger)
        {
            this.moduleReader = moduleReader;
            this.logger = logger;
        }

        public void Execute(string referenceName)
        {
            var referenceToRemove = moduleReader.Module.AssemblyReferences
				.Where(x => x.Name == referenceName)
				.FirstOrDefault();
			if (referenceToRemove == null)
			{
				logger.LogMessage(string.Format("\tNo reference to {0} found. References not modified.", referenceName));
				return;
			}

			moduleReader.Module.AssemblyReferences.Remove(referenceToRemove);
		    logger.LogMessage(string.Format("\tRemoving reference to {0}.", referenceName));
        }
	}
}